# Author:Yiye
# -*- codeing = utf-8 -*-
# @Time :2021/11/11 19:04
# @Author:李卓
# @Site : 
# @File : get豆瓣前十页.py
# @Software : PyCharm
import urllib.parse
import urllib.request

from pythonbug.redispool import redisUtils
def creat_request(page):
    base_url = 'https://movie.douban.com/j/new_search_subjects?sort=U&range=0,10&tags=%E9%9D%92%E6%98%A5'

    data = {
        'start': page+20
    }
    data = urllib.parse.urlencode(data)
    url = base_url + data
    print(url)
    # print(url)
    headers = {
        'Accept': ' application/json, text/plain, */*',
        # 'Accept-Encoding':' gzip, deflate, br',
        'Accept-Language': ' zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Connection': ' keep-alive',
        'Cookie':'douban-fav-remind=1; gr_user_id=77fe8d07-3c78-4ebd-997f-5fa8e76f478e; __utmv=30149280.2464; bid=gI_9wtGxfUM; ll="118106"; _vwo_uuid_v2=D20F8A3D1F50B8B0FE2A9ABAACE4BB452|e19f25957e253cdec5305bfd6807df04; dbcl2="207315804:0DZhwcnJSs8"; ck=Ng5H; __utma=30149280.1220103806.1587903272.1636618656.1636685438.16; __utmb=30149280.0.10.1636685438; __utmc=30149280; __utmz=30149280.1636685438.16.2.utmcsr=open.weixin.qq.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utma=223695111.915622484.1636618656.1636618656.1636685438.2; __utmb=223695111.0.10.1636685438; __utmc=223695111; __utmz=223695111.1636685438.2.2.utmcsr=open.weixin.qq.com|utmccn=(referral)|utmcmd=referral|utmcct=/; _pk_ref.100001.4cf6=["","",1636685438,"https://open.weixin.qq.com/"]; _pk_ses.100001.4cf6=*; push_noty_num=0; push_doumail_num=0; _pk_id.100001.4cf6=e44d2596b4e77944.1636618656.2.1636685495.1636619779.',
        'Host':'movie.douban.com',
        'Referer': 'https://movie.douban.com/tag/',
        'sec-ch-ua': ' "Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"',
        'sec-ch-ua-mobile': ' ?0',
        'sec-ch-ua-platform': ' "Windows"',
        'Sec-Fetch-Dest': ' empty',
        'Sec-Fetch-Mode': ' cors',
        'Sec-Fetch-Site': ' same-origin',
        'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36',
    }

    request = urllib.request.Request(url=url,headers=headers)
    return request


def get_content(request):
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf8')
    return content


def download(page,content):
    with open('douban' + str(page) + '.json','w',encoding='utf-8') as fp:
        fp.write(content)


def down_load_redis(content):
    r = redisUtils('47.94.155.194',6379,'java_redis_dev@password')
    r_counter = int(r.get_value('counter'))
    key = 'douban'+str(r_counter)+'.json'
    r.set_key_value(key,content)
    r.set_key_value('counter', str(r_counter+1))


if __name__ == '__main__':
    # start_page = int(input('请输入起始页:'))
    # end_page = int(input('请输入结束页:'))

    start_page = 20
    end_page = 21

    for page in range(start_page,end_page+1):
        requset = creat_request(page)
        urllib.request.urlopen(requset)
        content = get_content(requset)
        download(page,content)


